package com.hongsen.cn.activity;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.GridLayoutManager;

import com.hongsen.cn.Utils.LogUtil;
import com.hongsen.cn.Utils.RxBus;
import com.hongsen.cn.Utils.SPUtil;
import com.hongsen.cn.Utils.pinyin.TextPinyinUtil;
import com.hongsen.cn.adapter.SearchPoliceAdapter;
import com.hongsen.cn.base.BaseActivity;
import com.hongsen.cn.bean.BaseTitleBean;
import com.hongsen.cn.bean.PoliceListBean;
import com.hongsen.cn.bean.RxValue;
import com.hongsen.cn.bean.TitleBean;
import com.hongsen.cn.contract.SearchPersonView;
import com.hongsen.cn.dayandmonth.R;
import com.hongsen.cn.dayandmonth.databinding.ActivitySearchPersonBinding;
import com.hongsen.cn.listener.OnItemClickListener;
import com.hongsen.cn.presenter.SearchPersonPresenter;

import java.util.ArrayList;
import java.util.List;

public class SearchPresonActivity extends BaseActivity<SearchPersonPresenter> implements SearchPersonView {

    public ActivitySearchPersonBinding binding;
    public List<PoliceListBean> list = new ArrayList<>();
    public SearchPoliceAdapter adapter;
    public List<PoliceListBean> listPoliceBean = new ArrayList<>();//民警列表
    public List<PoliceListBean> listResultBean = new ArrayList<>();//搜索民警列表

    @Override
    protected SearchPersonPresenter createPresenter() {
        return new SearchPersonPresenter(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {


        binding = DataBindingUtil.inflate(LayoutInflater.from(this), R.layout.activity_search_person, null, false);
        binding.setActivity(this);
        baseFl.addView(binding.getRoot());
        presenter = new SearchPersonPresenter(this);
        presenter.getList();

        TitleBean titleBean = TitleBean.getBean(SearchPresonActivity.this);
        if (titleBean != null) {
            LogUtil.e("搜索界面发送title数据");

            RxBus.getDefault().post(new RxValue("module_FBI_WG", titleBean.getTitleOne()));
            RxBus.getDefault().post(new RxValue("module_FBI_JD", titleBean.getTitleTwo()));
            RxBus.getDefault().post(new RxValue("module_FBI_JD_CC", titleBean.getTitleThree()));
            RxBus.getDefault().post(new RxValue("module_FBI_DT", titleBean.getTitleFour()));
            RxBus.getDefault().post(new RxValue("module_FBI_CS", titleBean.getTitleFive()));
        } else {
            LogUtil.e("搜索界面titleBean== null");

        }

        binding.recyclerView.setLayoutManager(new GridLayoutManager(SearchPresonActivity.this, 4));
        binding.recyclerView.setItemAnimator(new DefaultItemAnimator());
        adapter = new SearchPoliceAdapter(this, list);
        binding.recyclerView.setAdapter(adapter);
        adapter.setOnItemClickLitener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(SearchPresonActivity.this, PoliceDescActivity.class);
                intent.putExtra("module_bean", list.get(position));
                startActivity(intent);
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });

        binding.btnClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                presenter.getList();
                binding.searchEtInput.setText("");
            }
        });

        binding.btnDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (binding.searchEtInput.getText().length() <= 0) {
                    return;
                }
                String str = binding.searchEtInput.getText().toString();
                str = str.substring(0, str.length() - 1);
                binding.searchEtInput.setText(str);
                if (binding.searchEtInput.getText().length() == 0) {
                    list.clear();
                    list.addAll(listPoliceBean);
                    adapter.notifyDataSetChanged();
                } else {
                    for (PoliceListBean bean : listPoliceBean) {
                        if (TextPinyinUtil.getInstance().getPinyin(bean.getMjxm()).contains(str)) {
                            listResultBean.add(bean);
                        }
                    }

                    if (listResultBean.size() != 0) {
                        list.clear();
                        list.addAll(listResultBean);
                        adapter.notifyDataSetChanged();
                    }
                }
            }
        });
    }

    /**
     * button点击事件
     *
     * @param v
     */
    public void buttonClick(View v) {
        String str = binding.searchEtInput.getText().toString();
        str = str + ((Button) v).getText();
        binding.searchEtInput.setText(str);
        listResultBean.clear();
        for (PoliceListBean bean : listPoliceBean) {
            if (TextPinyinUtil.getInstance().getPinyin(bean.getMjxm()).contains(str)) {
                listResultBean.add(bean);
            }
        }

        if (listResultBean.size() != 0) {
            list.clear();
            list.addAll(listResultBean);
            adapter.notifyDataSetChanged();
        }

    }

    public void onClick(View v) {
        int sS_ID = v.getId();
        if (sS_ID == R.id.btnBack) {
            finish();
        }
    }

    @Override
    public void getPolices(List<PoliceListBean> s) {
        this.listPoliceBean = s;
        listResultBean.clear();
        listResultBean.addAll(s);

        if (listResultBean.size() != 0) {
            list.clear();
            list.addAll(listResultBean);
            adapter.notifyDataSetChanged();
        }
    }


}
